home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / EDITORS / MEMACS / C / Main < prev    next >
Text File  |  1990-06-26  |  23KB  |  1,032 lines

  1. /*
  2.  *    MicroEMACS 3.11
  3.  *        written by Daniel M. Lawrence
  4.  *        based on code by Dave G. Conroy.
  5.  *
  6.  *    (C)Copyright 1988,1989,1990,1991 by Daniel M. Lawrence
  7.  *    MicroEMACS 3.11 can be copied and distributed freely for any
  8.  *    non-commercial purposes. MicroEMACS 3.11 can only be incorporated
  9.  *    into commercial software with the permission of the current author.
  10.  *
  11.  * This file contains the main driving routine, and some keyboard processing
  12.  * code, for the MicroEMACS screen editor.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17.  
  18. /* make global definitions not external */
  19. #define maindef
  20.  
  21. #include    "estruct.h"    /* global structures and defines */
  22. #include    "eproto.h"    /* variable prototype definitions */
  23. #include    "efunc.h"    /* function declarations and name table */
  24. #include    "edef.h"    /* global definitions */
  25. #include    "elang.h"    /* human language definitions */
  26. #include    "ebind.h"    /* default key bindings */
  27.  
  28. /* for many different systems, increase the default stack space */
  29.  
  30. #if    MSDOS & LATTICE
  31. unsigned _stack = 20000;
  32. #endif
  33.  
  34. #if    MSDOS & DTL
  35. int    _okbigbuf = 0;        /* Only allocate memory when needed.*/
  36. int    _stack = 20000;     /* Reset the ol' stack size.*/
  37. #endif
  38.  
  39. #if    ATARI & MWC
  40. long _stksize = 20000L;     /* reset stack size (must be even) */
  41. #endif
  42.  
  43. #if    MSDOS & AZTEC
  44. int _STKSIZ = 20000/16;     /* stack size in paragraphs */
  45. int _STKRED = 1024;        /* stack checking limit */
  46. int _HEAPSIZ = 4096/16;     /* (in paragraphs) */
  47. /*int _STKLOW = 0;        default is stack above heap (small only) */
  48. #endif
  49.  
  50. #if    MSDOS & TURBO
  51. extern unsigned int _stklen = 10000;
  52. #endif
  53.  
  54. #if    ARM
  55. extern char *dirscan (char *);
  56. #endif
  57.  
  58. /*    make VMS happy...    */
  59.  
  60. #if    VMS
  61. #include    <ssdef.h>
  62. #define GOOD    (SS$_NORMAL)
  63. #endif
  64.  
  65. #ifndef GOOD
  66. #define GOOD    0
  67. #endif
  68.  
  69. /*
  70.     This is the primary entry point that is used by command line
  71.     invocation, and by applications that link with microemacs in
  72.     such a way that each invocation of Emacs is a fresh environment.
  73.  
  74.     There is another entry point in VMS.C that is used when
  75.     microemacs is "linked" (In quotes, because it is a run-time link
  76.     rather than a link-time link.) with applications that wish Emacs
  77.     to preserve it's context across invocations.  (For example,
  78.     EMACS.RC is only executed once per invocation of the
  79.     application, instead of once per invocation of Emacs.)
  80.  
  81.     Note that re-entering an Emacs that is saved in a kept
  82.     subprocess would require a similar entrypoint.
  83. */
  84.  
  85. #if    CALLED
  86. emacs(argc, argv)
  87. #else
  88. #if    VMS
  89. me$edit(argc, argv)
  90. MAIN_PROGRAM
  91. #else
  92. main(argc, argv)
  93. #endif
  94. #endif
  95.  
  96. int argc;    /* # of arguments */
  97. char *argv[];    /* argument strings */
  98.  
  99. {
  100.     register int status;
  101.  
  102.     /* Initialize the editor */
  103.     eexitflag = FALSE;
  104.     vtinit();        /* Terminal */
  105.  
  106.     if (eexitflag)
  107.         goto abortrun;
  108.     edinit("main");     /* Buffers, windows, screens */
  109.  
  110.     varinit();        /* user variables */
  111.     initchars();        /* character set definitions */
  112.  
  113.     /* Process the command line and let the user edit */
  114.     dcline(argc, argv, TRUE);
  115.     status = editloop();
  116. abortrun:
  117.     vttidy();
  118. #if    CLEAN
  119.     clean();
  120. #endif
  121. #if    CALLED
  122.     return(status);
  123. #else
  124.     exit(status);
  125. #endif
  126. }
  127.  
  128. #if    CLEAN
  129. /*
  130.     On some primitive operation systems, and when emacs is used as
  131.     a subprogram to a larger project, emacs needs to de-alloc its
  132.     own used memory, otherwise we just exit.
  133. */
  134.  
  135. PASCAL NEAR clean()
  136. {
  137.     register BUFFER *bp;    /* buffer list pointer */
  138.     register SCREEN *scrp;    /* ptr to screen to dump */
  139.  
  140.     /* first clean up the screens */
  141.     scrp = first_screen;
  142.     while (wp) {
  143.         first_screen = sp->s_next_screen;
  144.         free_screen(scrp);
  145.         sp = first_screen;
  146.     }
  147.     cur_screen->s_first_window = cur_screen->w_cur_window = wheadp = NULL;
  148.  
  149.     /* then the buffers */
  150.     bp = bheadp;
  151.     while (bp) {
  152.         bp->b_nwnd = 0;
  153.         bp->b_flag = 0; /* don't say anything about a changed buffer! */
  154.         zotbuf(bp);
  155.         bp = bheadp;
  156.     }
  157.  
  158.     /* the screens */
  159.     /***************/
  160.  
  161.     /* and the kill buffer */
  162.     kdelete();
  163.  
  164.     /* clear some search variables */
  165. #if    MAGIC
  166.     mcclear();
  167.     rmcclear();
  168. #endif
  169.     if (patmatch != NULL) {
  170.         free(patmatch);
  171.         patmatch = NULL;
  172.     }
  173.  
  174.     /* dealloc the user variables */
  175.     varclean();
  176.  
  177.     /* and the video buffers */
  178.     vtfree();
  179. }
  180. #endif
  181.  
  182. /*    Process a command line.   May be called any time.    */
  183.  
  184. PASCAL NEAR dcline(argc, argv, firstflag)
  185.  
  186. int argc;
  187. char *argv[];
  188. int firstflag;    /* is this the first time in? */
  189.  
  190. {
  191.     register BUFFER *bp;        /* temp buffer pointer */
  192.     register int    firstfile;    /* first file flag */
  193.     register int    carg;        /* current arg to scan */
  194.     register int    startflag;    /* startup executed flag */
  195.     BUFFER *firstbp = NULL;     /* ptr to first buffer in cmd line */
  196.     int viewflag;            /* are we starting in view mode? */
  197.     int gotoflag;            /* do we need to goto a line at start? */
  198.     int gline;            /* if so, what line? */
  199.     int searchflag;         /* Do we need to search at start? */
  200.     int errflag;            /* C error processing? */
  201.     VDESC vd;            /* variable num/type */
  202.     char bname[NBUFN];        /* buffer name of file to read */
  203.  
  204. #if    CRYPT
  205.     int cryptflag;            /* encrypting on the way in? */
  206.     char ekey[NPAT];        /* startup encryption key */
  207. #endif
  208.     CONST NOSHARE extern *pathname[]; /* startup file path/name array */
  209.  
  210.     viewflag = FALSE;    /* view mode defaults off in command line */
  211.     gotoflag = FALSE;    /* set to off to begin with */
  212.     searchflag = FALSE;    /* set to off to begin with */
  213.     firstfile = TRUE;    /* no file to edit yet */
  214.     startflag = FALSE;    /* startup file not executed yet */
  215.     errflag = FALSE;    /* not doing C error parsing */
  216. #if    CRYPT
  217.     cryptflag = FALSE;    /* no encryption by default */
  218. #endif
  219.     disphigh = FALSE;    /* don't escape high bit characters */
  220.     lterm[0] = 0;        /* standard line terminators */
  221.  
  222.     /* Parse a command line */
  223.     for (carg = 1; carg < argc; ++carg) {
  224.  
  225.         /* Process Switches */
  226. #if WMCS
  227.         if (argv[carg][0] == ':') {
  228. #else
  229.         if (argv[carg][0] == '-') {
  230. #endif
  231.             switch (argv[carg][1]) {
  232.                 /* Process Startup macroes */
  233.                 case 'c':    /* -c for changable file */
  234.                 case 'C':
  235.                     viewflag = FALSE;
  236.                     break;
  237.                 case 'e':    /* -e process error file */
  238.                 case 'E':
  239.                     errflag = TRUE;
  240.                     break;
  241.                 case 'g':    /* -g for initial goto */
  242.                 case 'G':
  243.                     gotoflag = TRUE;
  244.                     gline = asc_int(&argv[carg][2]);
  245.                     break;
  246.                 case 'i':    /* -i<var> <value> set an initial */
  247.                 case 'I':    /* value for a variable */
  248.                     bytecopy(bname, &argv[carg][2], NVSIZE);
  249.                     findvar(bname, &vd, NVSIZE + 1);
  250.                     if (vd.v_type == -1) {
  251.                         mlwrite(TEXT52, bname);
  252. /*                            "%%No such variable as '%s'" */
  253.                         break;
  254.                     }
  255.                     svar(&vd, argv[++carg]);
  256.                     break;
  257. #if    CRYPT
  258.                 case 'k':    /* -k<key> for code key */
  259.                 case 'K':
  260.                     cryptflag = TRUE;
  261.                     strcpy(ekey, &argv[carg][2]);
  262.                     break;
  263. #endif
  264.                 case 'r':    /* -r restrictive use */
  265.                 case 'R':
  266.                     restflag = TRUE;
  267.                     break;
  268.                 case 's':    /* -s for initial search string */
  269.                 case 'S':
  270.                     searchflag = TRUE;
  271.                     bytecopy(pat,&argv[carg][2],NPAT);
  272.                     setjtable();
  273.                     break;
  274.                 case 'v':    /* -v for View File */
  275.                 case 'V':
  276.                     viewflag = TRUE;
  277.                     break;
  278.                 default:    /* unknown switch */
  279.                     /* ignore this for now */
  280.                     break;
  281.             }
  282.  
  283.         } else if (argv[carg][0]== '@') {
  284.  
  285.             /* Process Startup macroes */
  286.             if (startup(&argv[carg][1]) == TRUE)
  287.                 /* don't execute emacs.rc */
  288.                 startflag = TRUE;
  289.  
  290.         } else {
  291. #if    ARM
  292.             /* For the Archimedes, parse wildcards */
  293.             char *name;
  294.  
  295.             for (name = dirscan(argv[carg]); name; name = dirscan(0))
  296.             {
  297. #else
  298. #define    name    (argv[carg])
  299. #endif
  300.  
  301.             /* Process an input file */
  302.  
  303.             /* set up a buffer for this file */
  304.             makename(bname, name);
  305.             unqname(bname);
  306.  
  307.             /* set this to inactive */
  308.             bp = bfind(bname, TRUE, 0);
  309.             strcpy(bp->b_fname, name);
  310.             bp->b_active = FALSE;
  311.             if (firstfile) {
  312.                 firstbp = bp;
  313.                 firstfile = FALSE;
  314.             }
  315.  
  316.             /* set the modes appropriatly */
  317.             if (viewflag)
  318.                 bp->b_mode |= MDVIEW;
  319. #if    CRYPT
  320.             if (cryptflag) {
  321.                 bp->b_mode |= MDCRYPT;
  322.                 crypt((char *)NULL, 0);
  323.                 crypt(ekey, strlen(ekey));
  324.                 bytecopy(bp->b_key, ekey, NPAT);
  325.             }
  326. #endif
  327. #if    ARM
  328.             }
  329. #else
  330. #undef name
  331. #endif
  332.         }
  333.     }
  334.  
  335.     /* if we are C error parsing... run it! */
  336.     if (errflag) {
  337.         if (startup(ERRORCMD) == TRUE)
  338.             startflag = TRUE;
  339.     }
  340.  
  341.     /* if invoked with no other startup files,
  342.        run the system startup file here */
  343.     if (firstflag && startflag == FALSE)
  344.         startup("");
  345.  
  346.     /* if there are any files to read, read the first one! */
  347.     if (firstflag) {
  348.         bp = bfind("main", FALSE, 0);
  349.         if (firstfile == FALSE && (gflags & GFREAD)) {
  350.             swbuffer(firstbp);
  351.             curbp->b_mode |= gmode;
  352.             update(TRUE);
  353.             mlwrite(lastmesg);
  354.             zotbuf(bp);
  355.         } else
  356.             bp->b_mode |= gmode;
  357.     } else {
  358.         swbuffer(firstbp);
  359.         curbp->b_mode |= gmode;
  360.         update(TRUE);
  361.         mlwrite(lastmesg);
  362.     }
  363.  
  364.  
  365.     /* Deal with startup gotos and searches */
  366.     if (gotoflag && searchflag) {
  367.         update(FALSE);
  368.         mlwrite(TEXT101);
  369. /*            "[Can not search and goto at the same time!]" */
  370.     }
  371.     else if (gotoflag) {
  372.         if (gotoline(TRUE,gline) == FALSE) {
  373.             update(FALSE);
  374.             mlwrite(TEXT102);
  375. /*                "[Bogus goto argument]" */
  376.         }
  377.     } else if (searchflag) {
  378.         if (forwhunt(FALSE, 0) == FALSE)
  379.             update(FALSE);
  380.     }
  381.  
  382. }
  383.  
  384. /*
  385.     This is called to let the user edit something.    Note that if you
  386.     arrange to be able to call this from a macro, you will have
  387.     invented the "recursive-edit" function.
  388. */
  389.  
  390. PASCAL NEAR editloop()
  391.  
  392. {
  393.     register int c;         /* command character */
  394.     register int f;     /* default flag */
  395.     register int n;     /* numeric repeat count */
  396.     register int mflag;    /* negative flag on repeat */
  397.     register int basec;    /* c stripped of meta character */
  398.     register int oldflag;    /* old last flag value */
  399.  
  400.     /* setup to process commands */
  401.     lastflag = 0;        /* Fake last flags.    */
  402.  
  403. loop:
  404.     /* if we were called as a subroutine and want to leave, do so */
  405.     if (eexitflag)
  406.         return(eexitval);
  407.  
  408.     /* execute the "command" macro...normally null */
  409.     oldflag = lastflag;    /* preserve lastflag through this */
  410.     execkey(&cmdhook, FALSE, 1);
  411.     lastflag = oldflag;
  412.  
  413.     /* Fix up the screen    */
  414.     update(FALSE);
  415.  
  416.     /* get the next command from the keyboard */
  417.     c = getkey();
  418.  
  419.     /* if there is something on the command line, clear it */
  420.     if (mpresf != FALSE) {
  421.         mlerase();
  422.         update(FALSE);
  423.     }
  424.  
  425.     /* override the arguments if prefixed */
  426.     if (prefix) {
  427.         if (islower(c & 255))
  428.             c = (c & ~255) | upperc(c & 255);
  429.         c |= prefix;
  430.         f = predef;
  431.         n = prenum;
  432.         prefix = 0;
  433.     } else {
  434.         f = FALSE;
  435.         n = 1;
  436.     }
  437.  
  438.     /* do META-# processing if needed */
  439.  
  440.     basec = c & ~META;        /* strip meta char off if there */
  441.     if ((c & META) && ((basec >= '0' && basec <= '9') || basec == '-') &&
  442.         (getbind(c) == NULL)) {
  443.         f = TRUE;        /* there is a # arg */
  444.         n = 0;            /* start with a zero default */
  445.         mflag = 1;        /* current minus flag */
  446.         c = basec;        /* strip the META */
  447.         while ((c >= '0' && c <= '9') || (c == '-')) {
  448.             if (c == '-') {
  449.                 /* already hit a minus or digit? */
  450.                 if ((mflag == -1) || (n != 0))
  451.                     break;
  452.                 mflag = -1;
  453.             } else {
  454.                 n = n * 10 + (c - '0');
  455.             }
  456.             if ((n == 0) && (mflag == -1))    /* lonely - */
  457.                 mlwrite("Arg:");
  458.             else
  459.                 mlwrite("Arg: %d",n * mflag);
  460.  
  461.             c = getkey();    /* get the next key */
  462.         }
  463.         n = n * mflag;    /* figure in the sign */
  464.     }
  465.  
  466.     /* do ^U repeat argument processing */
  467.  
  468.     if (c == reptc) {           /* ^U, start argument   */
  469.         f = TRUE;
  470.         n = 4;                /* with argument of 4 */
  471.         mflag = 0;            /* that can be discarded. */
  472.         mlwrite("Arg: 4");
  473.         while ((c=getkey()) >='0' && c<='9' || c==reptc || c=='-') {
  474.             if (c == reptc)
  475.                 if ((n > 0) == ((n*4) > 0))
  476.                     n = n*4;
  477.                 else
  478.                     n = 1;
  479.             /*
  480.              * If dash, and start of argument string, set arg.
  481.              * to -1.  Otherwise, insert it.
  482.              */
  483.             else if (c == '-') {
  484.                 if (mflag)
  485.                     break;
  486.                 n = 0;
  487.                 mflag = -1;
  488.             }
  489.             /*
  490.              * If first digit entered, replace previous argument
  491.              * with digit and set sign.  Otherwise, append to arg.
  492.              */
  493.             else {
  494.                 if (!mflag) {
  495.                     n = 0;
  496.                     mflag = 1;
  497.                 }
  498.                 n = 10*n + c - '0';
  499.             }
  500.             mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  501.         }
  502.         /*
  503.          * Make arguments preceded by a minus sign negative and change
  504.          * the special argument "^U -" to an effective "^U -1".
  505.          */
  506.         if (mflag == -1) {
  507.             if (n == 0)
  508.                 n++;
  509.             n = -n;
  510.         }
  511.     }
  512.  
  513.     /* and execute the command */
  514.     execute(c, f, n);
  515.     goto loop;
  516. }
  517.  
  518. /*
  519.  * Initialize all of the buffers, windows and screens. The buffer name is
  520.  * passed down as an argument, because the main routine may have been told
  521.  * to read in a file by default, and we want the buffer name to be right.
  522.  */
  523.  
  524. PASCAL NEAR edinit(bname)
  525.  
  526. char bname[];    /* name of buffer to initialize */
  527.  
  528. {
  529.     register BUFFER *bp;
  530.  
  531.     /* initialize some important globals */
  532.  
  533.     readhook.k_ptr.fp = nullproc;    /* set internal hooks to OFF */
  534.     readhook.k_type = BINDFNC;
  535.     wraphook.k_ptr.fp = wrapword;
  536.     wraphook.k_type = BINDFNC;
  537.     cmdhook.k_ptr.fp = nullproc;
  538.     cmdhook.k_type = BINDFNC;
  539.     writehook.k_ptr.fp = nullproc;
  540.     writehook.k_type = BINDFNC;
  541.     bufhook.k_ptr.fp = nullproc;
  542.     bufhook.k_type = BINDFNC;
  543.     exbhook.k_ptr.fp = nullproc;
  544.     exbhook.k_type = BINDFNC;
  545.  
  546.     /* allocate the first buffer */
  547.     bp = bfind(bname, TRUE, 0);        /* First buffer     */
  548.     blistp = bfind("[List]", TRUE, BFINVS); /* Buffer list buffer    */
  549.     slistp = bfind("[Screens]", TRUE, BFINVS); /* Buffer list buffer    */
  550.     if (bp==NULL || blistp==NULL)
  551.         meexit(1);
  552.  
  553.     /* and allocate the default screen */
  554.     first_screen = init_screen("MAIN", bp);
  555.     if (first_screen == (SCREEN *)NULL)
  556.         meexit(1);
  557.  
  558.     /* set the current default screen/buffer/window */
  559.     cur_screen = first_screen;
  560.     scr_num = 1;
  561.     curbp = bp;
  562.     curwp = wheadp = cur_screen->s_cur_window = cur_screen->s_first_window;
  563. }
  564.  
  565. /*
  566.  * This is the general command execution routine. It handles the fake binding
  567.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  568.  * and arranges to move it to the "lastflag", so that the next command can
  569.  * look at it. Return the status of command.
  570.  */
  571. PASCAL NEAR execute(c, f, n)
  572.  
  573. int c;        /* key to execute */
  574. int f;        /* prefix argument flag */
  575. int n;        /* prefix value */
  576.  
  577. {
  578.     register int status;
  579.     KEYTAB *key;            /* key entry to execute */
  580. #if    DBCS
  581.     int schar;        /* second key in 2 byte sequence */
  582. #endif
  583.  
  584.     /* if the keystroke is a bound function...do it */
  585.     key = getbind(c);
  586.     if (key != NULL) {
  587.         thisflag = 0;
  588.         status = execkey(key, f, n);
  589.         lastflag = thisflag;
  590.         return(status);
  591.     }
  592.  
  593.     /*
  594.      * If a space was typed, fill column is defined, the argument is non-
  595.      * negative, wrap mode is enabled, and we are now past fill column,
  596.      * and we are not read-only, perform word wrap.
  597.      */
  598.     if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  599.         n >= 0 && getccol(FALSE) > fillcol &&
  600.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  601.         execkey(&wraphook, FALSE, 1);
  602.  
  603.     if ((c>=0x20 && c<=0xFF)) {    /* Self inserting.    */
  604.         if (n <= 0) {            /* Fenceposts.        */
  605.             lastflag = 0;
  606.             return(n<0 ? FALSE : TRUE);
  607.         }
  608.         thisflag = 0;            /* For the future.    */
  609.  
  610.         /* replace or overwrite mode, not at the end of a string */
  611.         if (curwp->w_bufp->b_mode & (MDREPL | MDOVER) &&
  612.             curwp->w_doto < curwp->w_dotp->l_used) {
  613.  
  614.             /* if we are in replace mode, or
  615.                (next char is not a tab or we are at a tab stop) */
  616.             if (curwp->w_bufp->b_mode & MDREPL ||
  617.                 (lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
  618.                 (curwp->w_doto) % tabsize == (tabsize - 1)))
  619.                         ldelete(1L, FALSE);
  620.         }
  621.  
  622.         /* do the appropriate insertion */
  623.         if (c == '}' && (curbp->b_mode & MDCMOD) != 0)
  624.             status = insbrace(n, c);
  625.         else if (c == '#' && (curbp->b_mode & MDCMOD) != 0)
  626.             status = inspound();
  627. #if    DBCS
  628.         else if (is2char(c)) {
  629.             schar = getkey();
  630.             status = TRUE;
  631.             while (n--) {
  632.                 if (linsert(1, c) == FALSE)
  633.                     status = FALSE;
  634.                 if (linsert(1, schar) == FALSE)
  635.                     status = FALSE;
  636.             }
  637.         }
  638. #endif
  639.  
  640.         else
  641.             status = linsert(n, c);
  642.  
  643.         /* check for CMODE fence matching */
  644.         if ((c == '}' || c == ')' || c == ']') &&
  645.                 (curbp->b_mode & MDCMOD) != 0)
  646.             fmatch(c);
  647.  
  648.         /* check auto-save mode */
  649.         if (curbp->b_mode & MDASAVE)
  650.             if (--gacount == 0) {
  651.                 /* and save the file if needed */
  652.                 upscreen(FALSE, 0);
  653.                 filesave(FALSE, 0);
  654.                 gacount = gasave;
  655.             }
  656.  
  657.         lastflag = thisflag;
  658.         return(status);
  659.     }
  660.     TTbeep();
  661.     mlwrite(TEXT19);        /* complain        */
  662. /*        "[Key not bound]" */
  663.     lastflag = 0;                /* Fake last flags.    */
  664.     return(FALSE);
  665. }
  666.  
  667. /*
  668.     Fancy quit command, as implemented by Norm. If the any buffer
  669. has changed do a write on that buffer and exit emacs, otherwise simply
  670. exit.
  671. */
  672.  
  673. PASCAL NEAR quickexit(f, n)
  674.  
  675. int f,n;    /* prefix flag and argument */
  676.  
  677. {
  678.     register BUFFER *bp;    /* scanning pointer to buffers */
  679.     register BUFFER *oldcb; /* original current buffer */
  680.     register int status;
  681.  
  682.     oldcb = curbp;                /* save in case we fail */
  683.  
  684.     bp = bheadp;
  685.     while (bp != NULL) {
  686.         if ((bp->b_flag&BFCHG) != 0    /* Changed.        */
  687.         && (bp->b_flag&BFINVS) == 0) {    /* Real.        */
  688.             curbp = bp;        /* make that buffer cur */
  689.             mlwrite(TEXT103,bp->b_fname);
  690. /*                "[Saving %s]" */
  691.             mlwrite("\n");
  692.             if ((status = filesave(f, n)) != TRUE) {
  693.                 curbp = oldcb;    /* restore curbp */
  694.                 return(status);
  695.             }
  696.         }
  697.     bp = bp->b_bufp;            /* on to the next buffer */
  698.     }
  699.     quit(f, n);                /* conditionally quit    */
  700.     return(TRUE);
  701. }
  702.  
  703. /*
  704.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  705.  * has been changed and not written out. Normally bound to "C-X C-C".
  706.  */
  707.  
  708. PASCAL NEAR quit(f, n)
  709.  
  710. int f,n;    /* prefix flag and argument */
  711.  
  712. {
  713.     register int status;    /* return status */
  714.  
  715.     if (f != FALSE        /* Argument forces it.    */
  716.     || anycb() == FALSE    /* All buffers clean or user says it's OK. */
  717.     || (status = mlyesno(TEXT104)) == TRUE) {
  718. /*                 "Modified buffers exist. Leave anyway" */
  719. #if    FILOCK
  720.         if (lockrel() != TRUE) {
  721.             TTputc('\n');
  722.             TTputc('\r');
  723.             TTclose();
  724.             TTkclose();
  725.             status = meexit(1);
  726.         }
  727. #endif
  728.         if (f)
  729.             status = meexit(n);
  730.         else
  731.             status = meexit(GOOD);
  732.     }
  733.     mlerase();
  734.     return(status);
  735. }
  736.  
  737. PASCAL NEAR meexit(status)
  738. int status;    /* return status of emacs */
  739. {
  740.     eexitflag = TRUE;    /* flag a program exit */
  741.     eexitval = status;
  742.  
  743.     /* and now.. we leave and let the main loop kill us */
  744.     return(TRUE);
  745. }
  746.  
  747. /*
  748.  * Begin a keyboard macro.
  749.  * Error if not at the top level in keyboard processing. Set up variables and
  750.  * return.
  751.  */
  752.  
  753. PASCAL NEAR ctlxlp(f, n)
  754.  
  755. int f,n;    /* prefix flag and argument */
  756.  
  757. {
  758.     if (kbdmode != STOP) {
  759.         mlwrite(TEXT105);
  760. /*            "%%Macro already active" */
  761.         return(FALSE);
  762.     }
  763.     mlwrite(TEXT106);
  764. /*        "[Start macro]" */
  765.     kbdptr = &kbdm[0];
  766.     kbdend = kbdptr;
  767.     kbdmode = RECORD;
  768.     return(TRUE);
  769. }
  770.  
  771. /*
  772.  * End keyboard macro. Check for the same limit conditions as the above
  773.  * routine. Set up the variables and return to the caller.
  774.  */
  775.  
  776. PASCAL NEAR ctlxrp(f, n)
  777.  
  778. int f,n;    /* prefix flag and argument */
  779.  
  780. {
  781.     if (kbdmode == STOP) {
  782.         mlwrite(TEXT107);
  783. /*            "%%Macro not active" */
  784.         return(FALSE);
  785.     }
  786.     if (kbdmode == RECORD) {
  787.         mlwrite(TEXT108);
  788. /*            "[End macro]" */
  789.         kbdmode = STOP;
  790.     }
  791.     return(TRUE);
  792. }
  793.  
  794. /*
  795.  * Execute a macro.
  796.  * The command argument is the number of times to loop. Quit as soon as a
  797.  * command gets an error. Return TRUE if all ok, else FALSE.
  798.  */
  799.  
  800. PASCAL NEAR ctlxe(f, n)
  801.  
  802. int f,n;    /* prefix flag and argument */
  803.  
  804. {
  805.     if (kbdmode != STOP) {
  806.         mlwrite(TEXT105);
  807. /*            "%%Macro already active" */
  808.         return(FALSE);
  809.     }
  810.     if (n <= 0)
  811.         return(TRUE);
  812.     kbdrep = n;        /* remember how many times to execute */
  813.     kbdmode = PLAY;     /* start us in play mode */
  814.     kbdptr = &kbdm[0];    /*    at the beginning */
  815.     return(TRUE);
  816. }
  817.  
  818. /*
  819.  * Abort.
  820.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  821.  * Sometimes called as a routine, to do general aborting of stuff.
  822.  */
  823.  
  824. PASCAL NEAR ctrlg(f, n)
  825.  
  826. int f,n;    /* prefix flag and argument */
  827.  
  828. {
  829.     TTbeep();
  830.     kbdmode = STOP;
  831.     mlwrite(TEXT8);
  832. /*        "[Aborted]" */
  833.     return(ABORT);
  834. }
  835.  
  836. /* tell the user that this command is illegal while we are in
  837.    VIEW (read-only) mode                */
  838.  
  839. PASCAL NEAR rdonly()
  840.  
  841. {
  842.     TTbeep();
  843.     mlwrite(TEXT109);
  844. /*        "[Key illegal in VIEW mode]" */
  845.     return(FALSE);
  846. }
  847.  
  848. PASCAL NEAR resterr()
  849.  
  850. {
  851.     TTbeep();
  852.     mlwrite(TEXT110);
  853. /*        "[That command is RESTRICTED]" */
  854.     return(FALSE);
  855. }
  856.  
  857. PASCAL NEAR nullproc(f, n)    /* user function that does NOTHING */
  858.  
  859. int n, f;    /* yes, these are default and never used.. but MUST be here */
  860.  
  861. {
  862. }
  863.  
  864. PASCAL NEAR meta(f, n)    /* set META prefixing pending */
  865.  
  866. int f, n;    /* prefix flag and argument */
  867.  
  868. {
  869.     prefix |= META;
  870.     prenum = n;
  871.     predef = f;
  872.     return(TRUE);
  873. }
  874.  
  875. PASCAL NEAR cex(f, n)    /* set ^X prefixing pending */
  876.  
  877. int f, n;    /* prefix flag and argument */
  878.  
  879. {
  880.     prefix |= CTLX;
  881.     prenum = n;
  882.     predef = f;
  883.     return(TRUE);
  884. }
  885.  
  886. PASCAL NEAR unarg()    /* dummy function for binding to universal-argument */
  887. {
  888. }
  889.  
  890. /*    bytecopy:    copy a string...with length restrictions
  891.             ALWAYS null terminate
  892. */
  893.  
  894. char *PASCAL NEAR bytecopy(dst, src, maxlen)
  895.  
  896. char *dst;    /* destination of copied string */
  897. char *src;    /* source */
  898. int maxlen;    /* maximum length */
  899.  
  900. {
  901.     char *dptr;    /* ptr into dst */
  902.  
  903.     dptr = dst;
  904.     while (*src && (maxlen-- > 0))
  905.         *dptr++ = *src++;
  906.     *dptr = 0;
  907.     return(dst);
  908. }
  909.  
  910. /*    copystr:    make another copy of the argument
  911.  
  912. */
  913.  
  914. char *PASCAL NEAR copystr(sp)
  915.  
  916. char *sp;    /* string to copy */
  917.  
  918. {
  919.     char *dp;    /* copy of string */
  920.  
  921.     /* make room! */
  922.     dp = malloc(strlen(sp)+1);
  923.     if (dp == NULL)
  924.         return(NULL);
  925.     strcpy(dp, sp);
  926.     return(dp);
  927. }
  928.  
  929. /*****        Compiler specific Library functions    ****/
  930.  
  931. #if    RAMSIZE
  932. /*    These routines will allow me to track memory usage by placing
  933.     a layer on top of the standard system malloc() and free() calls.
  934.     with this code defined, the environment variable, $RAM, will
  935.     report on the number of bytes allocated via malloc.
  936.  
  937.     with SHOWRAM defined, the number is also posted on the
  938.     end of the bottom mode line and is updated whenever it is changed.
  939. */
  940.  
  941. #undef    malloc
  942. #undef    free
  943.  
  944. #if     VMS & OPTMEM        /* these routines are faster! */
  945. #define    malloc    VAXC$MALLOC_OPT
  946. #define free    VAXC$FREE_OPT
  947. #endif
  948.  
  949. char *allocate(nbytes)    /* allocate nbytes and track */
  950.  
  951. unsigned nbytes;    /* # of bytes to allocate */
  952.  
  953. {
  954.     char *mp;    /* ptr returned from malloc */
  955.     char *malloc();
  956.     FILE *track;    /* malloc track file */
  957.  
  958.     mp = malloc(nbytes);
  959.  
  960. #if    RAMTRCK
  961.     track = fopen("malloc.dat", "a");
  962.     fprintf(track, "Allocating %u bytes at %u:%u\n", nbytes,
  963.             FP_SEG(mp), FP_OFF(mp));
  964.     fclose(track);
  965. #endif
  966.  
  967.     if (mp) {
  968. #if    0
  969.         envram += nbytes;
  970. #else
  971.         envram += 1;
  972. #endif
  973. #if    RAMSHOW
  974.         dspram();
  975. #endif
  976.     }
  977.  
  978.     return(mp);
  979. }
  980.  
  981. release(mp)    /* release malloced memory and track */
  982.  
  983. char *mp;    /* chunk of RAM to release */
  984.  
  985. {
  986.     unsigned *lp;    /* ptr to the long containing the block size */
  987. #if    RAMTRCK
  988.     FILE *track;    /* malloc track file */
  989.  
  990.     track = fopen("malloc.dat", "a");
  991.     fprintf(track, "Freeing %u:%u\n",
  992.             FP_SEG(mp), FP_OFF(mp));
  993.     fclose(track);
  994. #endif
  995.  
  996.     if (mp) {
  997.         /* update amount of ram currently malloced */
  998. #if    0
  999.         lp = ((unsigned *)mp) - 1;
  1000.         envram -= (long)*lp - 2;
  1001. #else
  1002.         envram -= 1;
  1003. #endif
  1004.         free(mp);
  1005. #if    RAMSHOW
  1006.         dspram();
  1007. #endif
  1008.     }
  1009. }
  1010.  
  1011. #if    RAMSHOW
  1012. dspram()    /* display the amount of RAM currently malloced */
  1013.  
  1014. {
  1015.     char mbuf[20];
  1016.     char *sp;
  1017.  
  1018.     TTmove(term.t_nrow - 1, 70);
  1019. #if    COLOR
  1020.     TTforg(7);
  1021.     TTbacg(0);
  1022. #endif
  1023.     sprintf(mbuf, "[%lu]", envram);
  1024.     sp = &mbuf[0];
  1025.     while (*sp)
  1026.         TTputc(*sp++);
  1027.     TTmove(term.t_nrow, 0);
  1028.     movecursor(term.t_nrow, 0);
  1029. }
  1030. #endif
  1031. #endif
  1032.